﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Specialized;

namespace IVTQLBH.UTILITIES
{
    public class TreeUtility
    {
        public static int _iParent = 0;
        public static int idem = 1;
        public string strLevel = "Level1";
        public string strID = "ID";
        public string strParentID = "ParentId";
        public string strOrder = "";
        //public static string strOrder = "Orders ASC ";

        public TreeUtility()
        {

        }
        public TreeUtility(string strLevel, string strID, string strParentID, string strOrder)
        {
            this.strID = strID;
            this.strLevel = strLevel;
            this.strOrder = strOrder;
            this.strParentID = strParentID;
        }

        #region
        public void BuildData(DataTable pdtbData, DataTable pdtbAllChild, DataRow pdrowNew, int pintLevel, int pintCCNID, int pintCategoryID)
        {
            try
            {
                DataRow drowItem = pdtbData.NewRow();
                foreach (DataColumn dcolData in pdtbData.Columns)
                    drowItem[dcolData.ColumnName] = pdrowNew[dcolData.ColumnName];
                drowItem[this.strLevel] = pintLevel;
                pdtbData.Rows.Add(drowItem);
                // get child
                //DataTable dtbChild = GetChild(pintCCNID, pintCategoryID, int.Parse(drowItem[ITM_ProductTable.PRODUCTID_FLD].ToString()));
                DataRow[] drowsChild = GetChild(pdtbAllChild, int.Parse(drowItem[this.strID].ToString()));
                foreach (DataRow drowChild in drowsChild)
                    BuildData(pdtbData, pdtbAllChild, drowChild, pintLevel + 1, pintCCNID, pintCategoryID);
            }
            catch (Exception ex)
            {
            }
        }
        private DataRow[] GetChild(DataTable pdtbAllChilds, int pintParentID)
        {
            try
            {
                DataRow[] drowResult = pdtbAllChilds.Select(this.strParentID + " = '" + pintParentID.ToString() + "'", this.strOrder);
                return drowResult;
            }
            catch (Exception ex)
            {
                DataRow[] drowResult = pdtbAllChilds.Select(this.strParentID + " = '" + pintParentID.ToString() + "'");
                return drowResult;
            }
        }

        public DataTable AddNumberedListToDataTable(DataTable pdtb, string _fieldName)
        {
            try
            {
                _iParent = 0;
                DataTable dtbRet = pdtb.Copy();

                DataColumn odcol = new DataColumn("NumberedList");
                odcol.DataType = typeof(string);
                odcol.DefaultValue = "";
                dtbRet.Columns.Add(odcol);

                int[] arriInputLevel = ExtractArrayOfLevelFromDataTable(pdtb);
                StringCollection arrNumberedList = GetNumberedList(arriInputLevel, 0, ".");

                int intCount = 0;
                foreach (DataRow row in dtbRet.Rows)
                {
                    string strNumber = arrNumberedList[intCount];
                    string strPartNo = row[_fieldName].ToString();
                    row["NumberedList"] = strNumber;
                    // indent the part no following the level
                    int intLevel = int.Parse(row[this.strLevel].ToString());
                    //if (intLevel == 0)
                    //{
                    //    _iParent++;
                    //    strPartNo = _iParent.ToString() + "  " + strPartNo;
                    //}
                    //for (int i = 0; i < intLevel; i++)
                    //    strPartNo = HttpUtility.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + row["NumberedList"] + "  " + strPartNo;
                    //row["ModulName"] = strPartNo;
                    intCount++;

                }
                return dtbRet;
            }
            catch
            {
                return null;
            }
        }

        public int[] ExtractArrayOfLevelFromDataTable(DataTable pdtb)
        {
            try
            {
                int[] arrintRet = new int[pdtb.Rows.Count];
                int intCount = 0;

                foreach (DataRow row in pdtb.Rows)
                {
                    arrintRet[intCount] = int.Parse(row[this.strLevel].ToString());
                    intCount++;
                }
                return arrintRet;
            }
            catch
            {
                return null;
            }
        }

        public StringCollection GetNumberedList(int[] parriInput, int pintRootNumber, string pstrDeli)
        {
            try
            {
                #region DEFINE Variables

                StringCollection arrRet = new StringCollection();
                int intRecordCount = parriInput.Length;
                StringCollection arrParentString = new StringCollection();
                for (int intCounter = 0; intCounter < intRecordCount + 1; intCounter++)
                {
                    arrParentString.Add("");
                }
                int[] arriLevelHit = new int[intRecordCount + 1];

                #endregion

                int intPrev = pintRootNumber; // in start phase, iRootNumber is iPrev
                foreach (int i in parriInput)
                {
                    string strOut = "";
                    /// Update level hit count == active running number of last index
                    (arriLevelHit[i])++; // increase the hit count  of level i
                    arriLevelHit[i + 1] = 0; // reset hit count of level i+1 to ZERO

                    if (i == pintRootNumber) // if the level is restart to iRootNumber
                    {
                        // level 0, not exist
                        // Parent string of level iRootNumber, alway = ""
                        // strOut always = "1"
                        arrParentString[i] = "";
                        strOut = idem.ToString();
                        // strOut = "1";
                        idem++;
                    }
                    else
                    {
                        strOut = arrParentString[i] + pstrDeli + arriLevelHit[i];
                        if (strOut.StartsWith("."))
                            strOut = strOut.Remove(0, 1);
                    }
                    intPrev = i;
                    arrParentString[i + 1] = strOut;
                    arrRet.Add(strOut);
                }
                return arrRet;
            }
            catch
            {
                return null;
            }
        }
        #endregion
    }
}
